package com.tuju.currency.web

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient
import com.tuju.currency.R
import kotlinx.android.synthetic.main.fragmeng_web_view.view.*

class WebViewFragment : Fragment() {

    lateinit var contentView: View
    lateinit var webView: WebView
    lateinit var tv_loading: TextView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        contentView = inflater.inflate(R.layout.fragmeng_web_view, container, false)
        webView = contentView.findViewById(R.id.webview)
        tv_loading = contentView.findViewById(R.id.tv_loading)
        return contentView
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initWebView()
        contentView.btn_back.setOnClickListener {
            if (webView.canGoBack()) {
                webView.goBack()
            }
        }
        val bundle = arguments
        if (bundle != null) {
            val url = bundle.getString("url")
            webView.loadUrl(url)
        }
    }

    private fun initWebView() {
        webView.webViewClient = object : WebViewClient() {
            //防止加载网页时调起系统浏览器
            override fun shouldOverrideUrlLoading(view: WebView, url: String?): Boolean {
                view.loadUrl(url)
                return true
            }
        }

        val ws = webView.settings
        // 网页内容的宽度是否可大于WebView控件的宽度
        ws.setLoadWithOverviewMode(false)
        // 保存表单数据
        ws.setSaveFormData(true)
        // 是否应该支持使用其屏幕缩放控件和手势缩放
        ws.setSupportZoom(true)
        ws.setBuiltInZoomControls(true)
        ws.setDisplayZoomControls(false)
        // 启动应用缓存
        ws.setAppCacheEnabled(true)
        // 设置缓存模式
        ws.setCacheMode(WebSettings.LOAD_DEFAULT)
        // setDefaultZoom  api19被弃用
        // 设置此属性，可任意比例缩放。
        ws.setUseWideViewPort(true)
        // 告诉WebView启用JavaScript执行。默认的是false。
        ws.setJavaScriptEnabled(true)
        //  页面加载好以后，再放开图片
        ws.setBlockNetworkImage(false)
        // 使用localStorage则必须打开
        ws.setDomStorageEnabled(true)
        // 排版适应屏幕
        ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS)
        // webview从5.0开始默认不允许混合模式,https中不能加载http资源,需要设置开启。
//		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//			ws.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
//		}
        /** 设置字体默认缩放大小(改变网页字体大小,setTextSize  api14被弃用)*/
        ws.setTextZoom(100)
        // 不缩放
        webView.setInitialScale(100)

        webView.webChromeClient = X5WebViewChromeClient()
    }

    inner class X5WebViewChromeClient : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)
            if (newProgress == 100) {
                tv_loading.visibility = View.GONE
            } else {
                tv_loading.visibility = View.VISIBLE
                tv_loading.text = "$newProgress%"
            }
        }
    }

    override fun onResume() {
        super.onResume()
        webView.onResume()
    }

    override fun onPause() {
        super.onPause()
        webView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        webView.clearHistory()
        val parent = webView.getParent() as ViewGroup
        parent?.removeView(webView)
        webView.removeAllViews()
        webView.stopLoading()
        webView.setWebChromeClient(null)
        webView.setWebViewClient(null)
        webView.destroy()
    }

    companion object {
        fun newInstance(url: String): WebViewFragment {
            val bundle = Bundle()
            bundle.putString("url", url)
            val fragment = WebViewFragment()
            fragment.arguments = bundle
            return fragment
        }
    }

}